package com.luojinboke.boke.dao;

import com.luojinboke.boke.domain.Blog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

public interface BlogRepository extends JpaRepository<Blog,Long>, JpaSpecificationExecutor<Blog> {


//    查找前几个
    @Query("select b from Blog b where b.recommend=true")
    List<Blog> findTop(Pageable pageable);


    @Query("select b from Blog b ")
    Map<String,Object> findTop1();


    @Query("select b from Blog b where b.title like ?1 or b.content like ?1")
    Page<Blog> findByQuery(String query,Pageable pageable);


    @Query("select function('date_format',b.updateTime,'%Y') as year from Blog b group by function('date_format',b.updateTime,'%Y') order by year desc ")
    List<String> findGroupYear();

    @Query("select b from Blog b where function('date_format',b.updateTime,'%Y') = ?1")
    List<Blog> findByYear(String year);

    @Modifying
    @Transactional
    @Query("delete from Blog b where b.type.id = ?1 ")
    void deleteByTypeId(Long id);
}
